package com.dtz.payment.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.dtz.payment.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class PaymentController {

	@Autowired
	private PaymentService paymentService;
	
	@GetMapping("/payment/hystrix/ok/{id}")
	public String paymentOk(@PathVariable("id") Long id) {
		String result = paymentService.ok(id);
		log.info("result = " + result);
		return result;
	}
	
	@GetMapping("/payment/hystrix/timeout/{id}")
	@HystrixCommand(fallbackMethod = "timeoutHandler", commandProperties = {
			// 超时时间， 5s内为正常逻辑
			@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "5500")
	})
	public String timeout(@PathVariable("id") Long id) {
		String result = paymentService.timeout(id);
		log.info("result = " + result);
		return result;
	}
	
	public String timeoutHandler(Long id) {
		return "线程" + Thread.currentThread().getId() + " 系统繁忙，请稍后再试；id = " + id;
	}
	
}
